// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   AudioManager.java

package com.cyberstep.onstage.audio;

import com.cyberstep.geometry.Vector3D;
import com.cyberstep.ring.util.ResourceManager;
import java.io.PrintStream;
import java.util.HashMap;

// Referenced classes of package com.cyberstep.onstage.audio:
//            JSAudio, SoundResource, Audio, KAudio, 
//            KAudio4Mac, Sound

public class AudioManager
{

    public AudioManager()
    {
    }

    public static void init()
    {
        if(!initialized)
        {
            Audio ja = new JSAudio();
            Sound s = ja.getSound("/sound/1.sr", (SoundResource)ResourceManager.getObject("/sound/1.sr"));
            if(s != null)
            {
                jaudio = ja;
            } else
            {
                System.err.println("AudioManager#JSAudio could not be initialized.");
                jaudio = null;
            }
            
            Audio ka;
            ka = new KAudioAL();
//            String os = System.getProperty("os.name");
//            if(os.startsWith("Windows"))
//                ka = new KAudio();
//            else
//            if(os.startsWith("Mac OS X"))
//                ka = new KAudio4Mac();
//            else
//                return;
            s = ka.getSound("/sound/1.sr", (SoundResource)ResourceManager.getObject("/sound/1.sr"));
            if(s != null)
            {
                kaudio = ka;
            } else
            {
                System.err.println("AudioManager#KAudio could not be initialized.");
                kaudio = null;
            }
            initialized = true;
        }
    }

    public static void setMasterVolume(float f)
    {
        if(jaudio != null)
            jaudio.setMasterVolume(f);
        if(kaudio != null)
            kaudio.setMasterVolume(f);
    }

    public static float getMasterVolume()
    {
        if(jaudio != null)
            return jaudio.getMasterVolume();
        else
            return 0.0F;
    }

    public static void preBufferJS(String soundname, SoundResource res, int numchannel)
    {
        if(jaudio != null)
        {
            jaudio.preBuffer(soundname, res, numchannel);
            if(!audiomap.containsKey(soundname))
                audiomap.put(soundname, jaudio);
        }
    }

    public static void preBufferK(String soundname, SoundResource res, int numchannel)
    {
        if(kaudio != null)
        {
            kaudio.preBuffer(soundname, res, numchannel);
            if(!audiomap.containsKey(soundname))
                audiomap.put(soundname, kaudio);
        }
    }

    public static Sound getSound(String soundname, SoundResource res)
    {
        Audio audio = (Audio)audiomap.get(soundname);
        if(audio != null)
            return audio.getSound(soundname, res);
        else
            return null;
    }

    public static Sound playSound(String soundname, float vol, float pt, int loop)
    {
        try
        {
            SoundResource sr = (SoundResource)ResourceManager.getObject(soundname);
            Sound s = null;
            if(sr != null && (s = getSound(soundname, sr)) != null)
            {
                s.setVolume(vol);
                s.setPitch(pt);
                s.play(loop);
                return s;
            }
            System.out.println("[AudioManager#playSound] " + soundname + " playing failed. " + sr + ", " + s);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static Vector3D getFocusViewLoc()
    {
        return focusViewLoc;
    }

    public static void setFocusViewLoc(Vector3D loc, String stageID)
    {
        if(stageID.startsWith("esp2_"))
            focusViewLoc = null;
        else
            focusViewLoc = loc;
    }

    private static Audio jaudio;
    private static Audio kaudio;
    static boolean initialized = false;
    static HashMap audiomap = new HashMap();
    public static Vector3D focusViewLoc;

}
